package com.owen.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.web.bindery.requestfactory.shared.*;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.web.bindery.event.shared.EventBus;
import com.owen.client.UIFactoryInstance;
import com.owen.liferay.shared.ClassProxy;
import com.owen.shared.AppRequestFactory;
import com.owen.shared.ClassRequest;
import com.owen.shared.events.AddNewClassEvent;
import com.owen.shared.events.DeleteClassEvent;
import com.owen.shared.events.EditClassEvent;
import com.owen.shared.events.SaveClassEvent;

public class ClassEditorPresenter {

	private final Display display;
	private final EventBus eventBus;
	private final AppRequestFactory requestFactory;
	private ClassProxy clazz;
	
	
	public interface Display extends IsWidget {
		
		String getClassId();
		String getClassName();
		String getTitle();
		Button getSubmit();
		void setClassId(String author);
		void setClassName(String publisher);
		void setTitle(String title);
		void clearAll();
	}

	public ClassEditorPresenter(final Display display, final EventBus eventBus,
			AppRequestFactory requestFactory) {
		
		super();
		this.display = display;
		this.eventBus = eventBus;
		this.requestFactory = requestFactory;
		
		setupEditHandler();
		
		setupSaveHandler();
		
		setupAddHandler();
		
		setupDeleteHandler();
		
		setupClickHandlers();
	}
	
	private void setupClickHandlers() {

		// manage the submit button action
		display.getSubmit().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new SaveClassEvent(display.getClassId(), 
						display.getClassName(), clazz.getId()));						
			}
			
		});
		
	}
	
	private void setupAddHandler() {
		eventBus.addHandler(AddNewClassEvent.TYPE, new AddNewClassEvent.Handler() {

			@Override
			public void addNewClass() {
				display.clearAll();
				display.asWidget().setVisible(true);
				display.setTitle("Add New Class...");
				ClassRequest saveReq = requestFactory.classRequest();
				clazz = saveReq.create(ClassProxy.class);
				
			}
		});
	}

	private void setupSaveHandler() {
		eventBus.addHandler(SaveClassEvent.TYPE, new SaveClassEvent.Handler() {

			@Override
			public void processEdit(final String classId, final String className, 
					final long id) {

				GWT.log("Processing save request");

				// if new author
				if (id == 0) {
					ClassRequest saveReq = requestFactory.classRequest();

					ClassProxy proxy = saveReq.create(ClassProxy.class);
					
					proxy.setClassId(classId);

					proxy.setClassName(className);

					Request<Void> saveRequest = saveReq.persist().using(proxy);

					saveRequest.fire(new Receiver<Void>() {

						@Override
						public void onSuccess(Void response) {
							Window.alert("Data has been saved...");
							display.asWidget().setVisible(false);
							display.clearAll();								
							UIFactoryInstance.getInstance().getClassPresenter().populateTable();
						}

					});
					
					
				} else {

					Request<ClassProxy> findReq = requestFactory.classRequest().findClass(clazz.getId());

					findReq.fire(new Receiver<ClassProxy>() {

						@Override
						public void onSuccess(ClassProxy clazz) {

							ClassRequest saveReq = requestFactory.classRequest();

							ClassProxy proxy = saveReq.edit(clazz);

							proxy.setClassId(classId);

							proxy.setClassName(className);

							Request<Void> saveRequest = saveReq.persist().using(proxy);

							saveRequest.fire(new Receiver<Void>() {

								@Override
								public void onSuccess(Void response) {
									Window.alert("Data has been saved...");
									display.asWidget().setVisible(false);
									display.clearAll();								
									UIFactoryInstance.getInstance().getClassPresenter().populateTable();
								}

							});

						}

					});
				}

			}

		});	
	}
	
	private void setupDeleteHandler() {
		eventBus.addHandler(DeleteClassEvent.TYPE, new DeleteClassEvent.Handler() {
			
			@Override
			public void processDelete(long id) {
				
				GWT.log("Preparing to remove record with id: " + id);
				
				Request<Void> delReq = requestFactory.classRequest().deleteRecord(id);
				
				delReq.fire(new Receiver<Void> () {

					@Override
					public void onSuccess(Void response) {
						Window.alert("Record has been removed...");
						display.asWidget().setVisible(false);
						display.clearAll();								
						UIFactoryInstance.getInstance().getClassPresenter().populateTable();
						
					}
					
				});
				
			}
		});
	}
	
	private void setupEditHandler() {
		// this event will populate the form with data and setup the event handler for the submit button
		eventBus.addHandler(EditClassEvent.TYPE, new EditClassEvent.Handler() {
			@Override
			public void processEdit(ClassProxy classToEdit) {
				clazz = classToEdit; 
				display.setTitle("Editing " + clazz.getClassName());
				display.setClassId(clazz.getClassId());
				display.setClassName(clazz.getClassName());
				display.asWidget().setVisible(true);
				
				
			}
			
		});
	}
	public Display getDisplay() {
		return display;
	}
	
	
}
